package com.xuecheng.manage_cms_client.service;

import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFS;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.CmsSite;
import com.xuecheng.framework.domain.cms.response.CmsCode;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.manage_cms_client.dao.CmsPageRepository;
import com.xuecheng.manage_cms_client.dao.CmsSiteRepository;
import io.swagger.models.auth.In;
import jdk.internal.util.xml.impl.Input;
import org.apache.commons.io.IOUtils;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Null;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

@Service
public class PageService {
    @Autowired
    CmsPageRepository cmsPageRepository;
    @Autowired
    CmsSiteRepository cmsSiteRepository;
    @Autowired
    GridFSBucket gridFSBucket;
    @Autowired
    GridFsTemplate gridFsTemplate;
//将页面保存到页面的物理路径
    public void savePageToServerPath(String pageId){
        Optional<CmsPage> optional = cmsPageRepository.findById(pageId);
        if (!optional.isPresent()){
            ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS);
        }
        //取出页面的物理路径
        CmsPage cmsPage = optional.get();
        //取出页面所属的站点id
        CmsSite cmsSite = this.getCmsSiteById(cmsPage.getSiteId());
        //拼接页面的物理路径
        String pagePath = cmsSite.getGetSitePhysicalPath() + cmsPage.getPagePhysicalPath()+ cmsPage.getPageName();
        //查询页面的静态文件
        String htmlFileId = cmsPage.getHtmlFileId(); //获取html文件的id
        InputStream inputStream = this.getFileById(htmlFileId); //根据id获取文件保存到输入流中
        if (inputStream == null){
            ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_HTMLISNULL);
        }
        FileOutputStream fileOutputStream = null;
        try {

            fileOutputStream = new FileOutputStream(new File(pagePath));
            //将文件保存到服务的物理路径中
            IOUtils.copy(inputStream,fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //根据文件的id获取文件内容
    public InputStream getFileById(String fileId){
        try {
            //根据页面的id查询到页面的内容
            GridFSFile id = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
            //打开一个下载流将文件内容下载下来根据获取到的内容的id
            GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(id.getObjectId());
            //打开操作流 继承自InputStreamResource 获取到页面内容
            GridFsResource gridFsResource = new GridFsResource(id, gridFSDownloadStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据站点id得到站点
    public CmsSite getCmsSiteById(String sitId){
        Optional<CmsSite> optional = cmsSiteRepository.findById(sitId);
        if (optional.isPresent()){
            CmsSite cmsSite = optional.get();
            return cmsSite;
        }
        return null;

    }


}
